mdToHml();
function mdToHml() {
	for (var i = 1; i <= $('.textarea').length; i++) {
		editormd.markdownToHTML("testEditorMdview"+i, {
		})
	}
}
layui.use(function () {
	var layer = layui.layer;
	var util = layui.util;
	var tipsIndex;
	var helper = 0;
	var rounds = $('.round span').length;
	var roundactive = $('.round span.active').index()
	const detection = $('.detection');
	const summarizeButton = $('.summarize');
	const nextButton = $('.nextQuestion');
	const restartButton = $('.restart');
	const practice = $('.practice');
	const answer = document.querySelector('.answer');

	layui.code({
		elem: '.code-demo'
	});
	util.on('lay-on', {
		'tips-correct': function () {
			tipsIndex = layer.tips('<img width="64" height="64" src="/public/images/exercise-correct.svg"><div class="info"><h3>真棒！</h3><p>继续努力。</p></div>', this, {
				tips: [1],
				skin: 'skin-tips',
				time: 0,
				closeBtn: 1,
				yes: function (index) {
					layer.close(index);
				}
			});
		},
		'tips-error': function () {
			tipsIndex = layer.tips('<img width="64" height="64" src="/public/images/exercise-try-again.svg"><div class="info"><h3>不太对！</h3><p>再试一次, <a href="javascript:;" id="helpLink">获取帮助</a></p></div>', this, {
				tips: [1],
				skin: 'skin-tips',
				time: 0,
				closeBtn: 1,
				yes: function (index) {
					layer.close(index);
				}
			});
			const helpLink = document.getElementById('helpLink');
			helpLink.addEventListener('click', () => {
				answer.style.display = 'block';
				helper = 1;
			});
		},
	});

	$('.select ul li').on('click', function () {
		$('.select ul li').removeClass('selected');
		$(this).addClass('selected');
		if ($('.select ul li.selected').length > 0) {
			$('.detection').removeClass('disable');
		}
	});

	detection.on('click', function () {
		var answer = $('.select ul li.selected').index();
		$.ajax({
			url: '/index/sub/submit_ques',
			type: 'post',
			async: false,
			data:{'id':practice.data('id'),'answer':answer,'course':practice.data('course'),'helper': helper},
			success:function(data){
				if(data.code == 200){
					if (data.result.status == 1){
						detection.attr('lay-on', 'tips-correct');
						$('prompt a').attr('href','/learning/'+data.result.course+'?seek='+data.result.seek);
						detection.hide();
						if (roundactive+1 == rounds){
							$('.summarize-box h3').text(data.points+'/'+data.counts+' 正确 · '+data.points+'个掌握点');
							summarizeButton.show();
						}else{
							nextButton.show();
						}
					}else{
						detection.attr('lay-on', 'tips-error');
					}
				}
			}
		});
	});
	nextButton.on('click', function () {
		var sort = practice.data('sort');
		question(sort);
	});
	restartButton.on('click', function () {
		layer.confirm('重新开始？', function(index){
			question(0);
			layer.close(index);
		});
	});
	summarizeButton.on('click', function () {
		$('.summarize-wrapper').show();
	});
});
function question(sort = 1){
	var course = $('.practice').data('course');
	var thiscity = '/learning/pages/'+course+'/practice.html?sort='+(sort+1);
	$.ajax({
		url: thiscity, 
		method: "GET",
		success: function(data) {
			$(".learning-content .content-panel").html(data);
		}
	});
}